﻿using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using XF.Common;
using XF.Common.Redis;
using XF.Common.WeChat;
using XF.Meeting.Common;
using XF.Meeting.DAL;
using XF.Meeting.Entity.CommonEntity.wx;
using XF.Meeting.Entity.View;

namespace QuartzJob.Jobs
{
    public class SendWxMsg : IJob
    {
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                var dal = new MeetingDal();
                var aa = dal.GetUserListForWX(DateTime.Now, out string StrError);
                if (StrError == "")
                {
                    var list1 = XF.Common.TableToList.ToList<meetIdS>(aa.Result);
                    if (list1.Count > 0)
                    {
                        foreach (var item in list1)
                        {
                            SendWxMssage(item.ID, true);
                        }
                        return Task.CompletedTask;
                    }
                    else
                    {
                        return Task.CompletedTask;
                    }
                }
                return Task.CompletedTask;
            }
            catch (Exception e)
            {

                return Task.CompletedTask;
            }

        }
        public static void SendWxMssage(int ID, bool isTen = false)
        {
            var dal = new MeetingDal();
            var res = dal.GetCInfoByID(ID).Result;

            var list = XF.Common.TableToList.ToList<XF.Meeting.Entity.View.MeetingInfoSingle>(res);
            var meetingInfoSingle = list[0];
            meetingInfoSingle.date = Convert.ToDateTime(meetingInfoSingle.BeginTime).ToString("yyyy-MM-dd");
            meetingInfoSingle.Begin = Convert.ToDateTime(meetingInfoSingle.BeginTime).ToString("HH:mm");
            meetingInfoSingle.End = Convert.ToDateTime(meetingInfoSingle.EndTime).ToString("HH:mm");
            meetingInfoSingle.CRooms = meetingInfoSingle.CRoom + "," + meetingInfoSingle.CRooms;
            var msgList = dal.getMeetInfoAndUserPhone(ID, out string StrError);
            var phoneList = XF.Common.TableToList.ToList<meetIdS>(msgList.Tables[0]);
            var phone = phoneList.Select(i => i.Account).ToList();
            var userr = GetUserString(phone);
            var wx = new WeChatHelper();
            var msg = new Message(new Text($"由{meetingInfoSingle.CoordinatorName}召集的会议《{meetingInfoSingle.ConferenceTheme}》，将于 {meetingInfoSingle.date}  {meetingInfoSingle.Begin}，在{meetingInfoSingle.Remark}{meetingInfoSingle.CRoom}召开。现在距离开会还有10分钟，请您准时出席。"), userr);

            wx.SendTextMessage(msg);
            dal.updateWxState(ID, out string strError);


        }
        public static string GetUserString(List<string> userList)
        {
            StringBuilder sb = new StringBuilder();

            foreach (var item in userList)
            {
                //if (string.IsNullOrWhiteSpace(RedisCache.Hash_Get<string>("UserPhone", item)))
                //{
                //    continue;
                //}

                sb.Append(item);
                sb.Append("|");
            }
            sb.Remove(sb.Length - 1, 1);
            return sb.ToString();
        }
        public class meetIdS
        {
            public int ID { get; set; }
            public string Phone { get; set; }
            public string Account { get; set; }
        }

    }

}
